package com.bio.action.product;

import java.io.Serializable;
import java.util.Calendar;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.bio.action.AbstractAction;
import com.bio.entity.product.Product;
import com.bio.service.product.ProductService;

@InterceptorRefs(value = { @InterceptorRef(value="fileUpload") ,@InterceptorRef(value="defaultStack")})
@Namespace("/product")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/product/listProducts.jsp", type = "dispatcher"),
		@Result(name = "listDetail", location = "/WEB-INF/views/product/listDetail.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "product!list", type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/product/editProduct.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/product/addProduct.jsp", type = "dispatcher")
		})
public class ProductAction extends AbstractAction<Product, ProductService> implements Serializable{

	private static final long serialVersionUID = 3793971919714018749L;

	public ProductAction(ProductService service) {
		super(service);
	}
	
	private Product product;

	public String list(){
		pagination.setCurrentIndex(getCurrentIndex());
		pager = service.findAllByPager(pagination);
		return "list";
	}
	
	public String listDetail(){
		this.product = service.get(id);
		return "listDetail";
	}
	
	public String add(){
		return "add";
	}
	
	public String edit(){
		this.product = this.service.get(id);
		return "edit";
	}
	
	public String save(){
		Calendar now = Calendar.getInstance();
		this.product.setCreatedDate(now);
		service.save(this.product);
		return "tolist";
	}
	
	public String update(){
		Product old = this.service.get(product.getId());
		
		this.product.setUpdatedDate(Calendar.getInstance());
		this.product.setCreatedDate(old.getCreatedDate());
        this.product.setAttachmentsInfo(old.getAttachmentsInfo());
        
		this.service.save(product);
		return "tolist";
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
}
